package com.hsgene.message.controller;

import com.hsgene.common.util.exception.ErrorCode;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.message.service.MqService;
import com.hsgene.product.applets.goods.AppletsErrorCode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 消息队列
 *
 * @author wxf
 * @date 2018/10/26 16:10
 **/
@RestController
@RequestMapping("/v1.0/genetic/testing/mq")
public class MqController {
    /**
     * Log
     */
    private static Logger LOGGER = LogManager.getLogger(MqController.class);

    @Resource
    private MqService mqService;

    /**
     * 发送消息队列
     *
     * @param content
     * @return
     */
    @PostMapping("send")
    public ApiResult sendMQ(@RequestBody Map<String, Object> content) {
        try {
            if (content == null) {
                return ApiResult.fail(AppletsErrorCode.MQ_MESSAGE_IS_NOT_NULL);
            }
            if (content.get("tagType") == null) {
                return ApiResult.fail(AppletsErrorCode.MQ_MESSAGE_TAG_IS_NOT_NULL);
            }
            ErrorCode sendResult = mqService.sendMQ(content);
            if (sendResult == null) {
                return new ApiResult(200, "success", "发送成功", sendResult);
            }
            return ApiResult.fail(sendResult);
        } catch (Exception e) {
            LOGGER.error("send mq is error...", e);
            return ApiResult.fail(AppletsErrorCode.SEND_MQ_IS_ERROR);
        }
    }

}
